package sgi.client;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.Date;


public class SGICheckReportsUI extends JFrame {
	
	private JDatePickerImpl datePicker;
	private SpringLayout sl_datePicker;
	private SpringLayout springLayout;
	private JPanel contentPane;
	private JButton btnComplaintReport;
	private JButton btnDatabaseReport;
	private JButton btnPackageReport;
	private JButton btnWeeklyStats;
	private JButton btnYearlyStats;
	
	private static SGICheckReportsController checkReport;
	private JButton btnBack;
	
	public SGICheckReportsUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setTitle("SGI Check Reports");
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane = new JPanel();
		contentPane.setLayout(gridBagLayout);
		setContentPane(contentPane);
		setBounds(100, 100, 465, 409);
		
		
		UtilDateModel model = new UtilDateModel();
		JDatePanelImpl datePanel = new JDatePanelImpl(model);
		datePicker = new JDatePickerImpl(datePanel);
		springLayout = new SpringLayout();
		springLayout.putConstraint(SpringLayout.NORTH, datePicker.getJFormattedTextField(), 0, SpringLayout.NORTH, datePicker);
		springLayout.putConstraint(SpringLayout.WEST, datePicker.getJFormattedTextField(), -90, SpringLayout.EAST, datePicker);
		springLayout.putConstraint(SpringLayout.SOUTH, datePicker.getJFormattedTextField(), 23, SpringLayout.NORTH, datePicker);
		springLayout.putConstraint(SpringLayout.EAST, datePicker.getJFormattedTextField(), -35, SpringLayout.EAST, datePicker);
		sl_datePicker = (SpringLayout) datePicker.getLayout();
		datePicker.setBounds(388, 23, 109, 31);
		GridBagConstraints gbc_datePicker = new GridBagConstraints();
		gbc_datePicker.insets = new java.awt.Insets(0, 0, 5, 5);
		gbc_datePicker.gridx = 2;
		gbc_datePicker.gridy = 1;
		contentPane.add(datePicker, gbc_datePicker);
		
		JButton btnDailyReport = new JButton("Daily Statistics Report");
		btnDailyReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Date date = (Date)datePicker.getModel().getValue();
				if (date != null) {
					try {
						checkReport.report(date, 0);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		GridBagConstraints gbc_btnDailyReport = new GridBagConstraints();
		gbc_btnDailyReport.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnDailyReport.insets = new Insets(0, 0, 5, 5);
		gbc_btnDailyReport.gridx = 1;
		gbc_btnDailyReport.gridy = 4;
		contentPane.add(btnDailyReport, gbc_btnDailyReport);
		
		btnComplaintReport = new JButton("Monthly Complaints Report");
		btnComplaintReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Date date = (Date)datePicker.getModel().getValue();
				if (date != null) {
					try {
						checkReport.report(date, 1);
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		GridBagConstraints gbc_btnComplaintReport = new GridBagConstraints();
		gbc_btnComplaintReport.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnComplaintReport.insets = new Insets(0, 0, 5, 5);
		gbc_btnComplaintReport.gridx = 1;
		gbc_btnComplaintReport.gridy = 7;
		contentPane.add(btnComplaintReport, gbc_btnComplaintReport);
		
		btnDatabaseReport = new JButton("Monthly Database State Report");
		btnDatabaseReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Date date = (Date)datePicker.getModel().getValue();
				if (date != null) {
					try {
						checkReport.report(date, 2);
					} catch (SQLException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
				}
			}
		});
		GridBagConstraints gbc_btnDatabaseReport = new GridBagConstraints();
		gbc_btnDatabaseReport.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnDatabaseReport.insets = new Insets(0, 0, 5, 5);
		gbc_btnDatabaseReport.gridx = 1;
		gbc_btnDatabaseReport.gridy = 9;
		contentPane.add(btnDatabaseReport, gbc_btnDatabaseReport);
		
		btnPackageReport = new JButton("Monthly Package Usage Report");
		btnPackageReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Date date = (Date)datePicker.getModel().getValue();
				if (date != null) {
					try {
						checkReport.report(date, 3);
					} catch (SQLException e3) {
						// TODO Auto-generated catch block
						e3.printStackTrace();
					}
				}
			}
		});
		GridBagConstraints gbc_btnPackageReport = new GridBagConstraints();
		gbc_btnPackageReport.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnPackageReport.insets = new Insets(0, 0, 5, 5);
		gbc_btnPackageReport.gridx = 1;
		gbc_btnPackageReport.gridy = 11;
		contentPane.add(btnPackageReport, gbc_btnPackageReport);
		
		btnWeeklyStats = new JButton("Weekly Statistics Report");
		btnWeeklyStats.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Date date = (Date)datePicker.getModel().getValue();
				if (date != null) {
					try {
						checkReport.report(date, 4);
					} catch (SQLException e4) {
						// TODO Auto-generated catch block
						e4.printStackTrace();
					}
				}
			}
		});
		GridBagConstraints gbc_btnWeeklyStats = new GridBagConstraints();
		gbc_btnWeeklyStats.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnWeeklyStats.insets = new Insets(0, 0, 5, 5);
		gbc_btnWeeklyStats.gridx = 1;
		gbc_btnWeeklyStats.gridy = 14;
		contentPane.add(btnWeeklyStats, gbc_btnWeeklyStats);
		
		btnYearlyStats = new JButton("Yearly Statistics Report");
		btnYearlyStats.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Date date = (Date)datePicker.getModel().getValue();
				if (date != null) {
					try {
						checkReport.report(date, 5);
					} catch (SQLException e5) {
						// TODO Auto-generated catch block
						e5.printStackTrace();
					}
				}
			}
		});
		GridBagConstraints gbc_btnYearlyStats = new GridBagConstraints();
		gbc_btnYearlyStats.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnYearlyStats.insets = new Insets(0, 0, 5, 5);
		gbc_btnYearlyStats.gridx = 1;
		gbc_btnYearlyStats.gridy = 16;
		contentPane.add(btnYearlyStats, gbc_btnYearlyStats);
		
		btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SGICheckReportsController.terminate();
			}
		});
		GridBagConstraints gbc_btnBack = new GridBagConstraints();
		gbc_btnBack.anchor = GridBagConstraints.EAST;
		gbc_btnBack.insets = new Insets(0, 0, 5, 5);
		gbc_btnBack.gridx = 2;
		gbc_btnBack.gridy = 20;
		contentPane.add(btnBack, gbc_btnBack);
	}

	public void setController(SGICheckReportsController checkReport)
	{
		SGICheckReportsUI.this.checkReport = checkReport;
	}
	
}
